package com.david.core.utils;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

public class CommonUtils {

    private static long lastClickTime = 0L;

    public static boolean isFastClick() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime < 300) {
            return true;
        }
        lastClickTime = currentTime;
        return false;
    }


    /**
     * Gets the corresponding path to a file from the given content:// URI
     * @param selectedVideoUri The content:// URI to find the file path from
     * @param contentResolver The content resolver to use to perform the query.
     * @return the file path as a string
     */
    public static String getMediaFilePathFromContentUri(Uri selectedVideoUri,
                                                   ContentResolver contentResolver) {
        String filePath;
        String[] filePathColumn = {MediaStore.MediaColumns.DATA};

        Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
//	    也可用下面的方法拿到cursor
//	    Cursor cursor = this.context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    }

    public static String getDocFilePathFromContentUri(Uri selectedVideoUri,
                                                        ContentResolver contentResolver) {
        String filePath;
        String[] filePathColumn = {
                DocumentsContract.Document.COLUMN_DOCUMENT_ID,
                DocumentsContract.Root.COLUMN_ROOT_ID,
                DocumentsContract.Root.COLUMN_TITLE,
                DocumentsContract.Root.COLUMN_AVAILABLE_BYTES,
                DocumentsContract.Document.COLUMN_DISPLAY_NAME,
                DocumentsContract.Document.MIME_TYPE_DIR,
                DocumentsContract.Document.COLUMN_SUMMARY
        };

        Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
//	    也可用下面的方法拿到cursor
//	    Cursor cursor = this.context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    }
    public static String formatMediaPlayTime(int time) {
        return (time / 60 > 9 ? time / 60 : ("0" + time / 60)) + ":" + (time % 60 > 9 ? time % 60 : ("0" + time % 60));
    }

}
